package org.linlinjava.litemall.web.controller;

import javax.validation.Valid;

import io.swagger.annotations.*;
import org.linlinjava.litemall.core.exception.BusinessRuntimeException;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.util.ResponseUtil.Response;
import org.linlinjava.litemall.db.beans.Constants;
import org.linlinjava.litemall.db.dto.WebOrderGoodsRemarkDTO;
import org.linlinjava.litemall.web.annotation.LogAnno;
import org.linlinjava.litemall.web.annotation.LoginShop;
import org.linlinjava.litemall.web.annotation.LoginUser;
import org.linlinjava.litemall.web.dto.AdminOrderGoodsReplaceAccessoryDTO;
import org.linlinjava.litemall.web.dto.WebOrderGoodsCheckOut;
import org.linlinjava.litemall.web.service.WebOrderGoodsService;
import org.linlinjava.litemall.web.vo.OrderDetailResponseVO;
import org.linlinjava.litemall.web.vo.OrderGoodsAddRequestVO;
import org.linlinjava.litemall.web.vo.OrderGoodsRecipeResponseVO;
import org.linlinjava.litemall.web.vo.OrderGoodsUpdateNumberRequestVO;
import org.linlinjava.litemall.web.vo.cook.UpdateCookReqVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * 网上订货控制器
 *
 * @author yliyun
 * @date 2022/09/14
 */
@Api(tags="订单商品操作API")
@RestController
@RequestMapping("/web/order/goods")
@Validated
public class WebOrderGoodsController {

    @Autowired
    private WebOrderGoodsService orderGoodsService;

    @ApiOperation(value="添加订单商品")
    @PostMapping("add")
    @LogAnno
    public Response<OrderDetailResponseVO> add(@ApiParam(name="shopId",value="登录的店铺id",required=true) @LoginShop Integer shopId,
                                               @Valid @RequestBody @ApiParam(name="request",value="添加商品请求对象",required=true) OrderGoodsAddRequestVO request) {
    	try {
    		Response<OrderDetailResponseVO> response = orderGoodsService.add(shopId, request);
    		return response;
		} catch (BusinessRuntimeException e) {
			return ResponseUtil.fail(e.getCode(), e.getMessage());
		}
    }

    @ApiOperation("订单商品结算")
    @PostMapping("/goodsCheckOut")
    @LogAnno
    public Response GoodsCheckOut(@ApiIgnore @LoginShop Integer shopId,@ApiIgnore @LoginUser Integer userId,
                                  @Valid @RequestBody WebOrderGoodsCheckOut orderGoodsCheckOut){
        return orderGoodsService.goodsCheckOut(shopId,userId,orderGoodsCheckOut);
    }

    /**
     * 更新
     * 修改订单商品
     *
     * @param shopId  商店id
     * @param request 请求
     * @return {@link Response}<{@link OrderDetailResponseVO}>
     */
    @ApiOperation(value="修改订单商品")
    @PutMapping("update")
    @LogAnno
    public Response<OrderDetailResponseVO> update(@ApiParam(name="shopId",value="登录的店铺id",required=true) @LoginShop Integer shopId,
                                                  @Valid @RequestBody @ApiParam(name="request",value="添加商品请求对象",required=true) OrderGoodsAddRequestVO request) {
        try {
            Response<OrderDetailResponseVO> response = orderGoodsService.update(shopId, request);
            return response;
        } catch (BusinessRuntimeException e) {
            return ResponseUtil.fail(e.getCode(), e.getMessage());
        }
    }


    /**
     * 删除
     * 删除订单商品
     *
     * @param orderGoodsId 订单商品id
     * @return {@link Response}<{@link OrderDetailResponseVO}>
     */
    @ApiOperation(value="删除订单商品")
    @GetMapping("delete")
    @LogAnno
    public Response<OrderDetailResponseVO> delete(@ApiParam(name="orderGoodsId",value="订单商品id",required=true) @RequestParam Integer orderGoodsId) {
    	return orderGoodsService.delete(orderGoodsId);
    }


    /**
     * 订单商品添加备注信息
     *
     * @param remark 备注
     * @return {@link Response}
     */
    @ApiOperation(value = "订单商品添加备注信息")
    @GetMapping("inserRemark")
    @LogAnno
    public Response inserRemark(@RequestParam("remark")String remark,@RequestParam("orderGoodsId") Integer orderGoodsId){
        return orderGoodsService.inserRemark(remark,orderGoodsId);
    }


    /**
     * 按订单id删除
     * 删除订单商品
     *
     * @param orderId 订单id
     * @return {@link Object}
     */
    @ApiOperation(value="删除全部订单商品")
    @GetMapping("deleteByOrderId")
    @LogAnno
    public Object deleteByOrderId(@ApiParam(name="orderId",value="订单id",required=true) @RequestParam Integer orderId) {
        return orderGoodsService.deleteByOrderId(orderId);
    }


    /**
     * 更新数量
     * 修改订单商品数量
     *
     * @param shopId  商店id
     * @param request 请求
     * @return {@link Response}<{@link OrderDetailResponseVO}>
     */
    @ApiOperation(value="修改订单商品数量")
    @PostMapping("update_number")
    @LogAnno
    public Response<OrderDetailResponseVO> updateNumber(@ApiParam(name="shopId",value="登录的店铺id",required=true) @LoginShop Integer shopId,
                                                        @Valid @RequestBody @ApiParam(name="request",value="修改订单商品数量请求对象",required=true) OrderGoodsUpdateNumberRequestVO request) {
    	try {
    		Response<OrderDetailResponseVO> response = orderGoodsService.updateNumber(shopId, request);
    		return response;
		} catch (BusinessRuntimeException e) {
			return ResponseUtil.fail(e.getCode(), e.getMessage());
		}
    }

    /**
     * 开始制作
     *
     * @param orderGoodsId 订单ID
     * @param userId       用户id
     * @return {@link Response}<{@link Object}>
     */
    @ApiOperation(value="开始制作")
    @GetMapping("start_cook")
    @LogAnno
    public Response<Object> startCook(@LoginUser Integer userId, @ApiParam(name="orderGoodsId",value="订单商品id",required=true) @RequestParam Integer orderGoodsId,@RequestParam Integer index) {
    	return orderGoodsService.updateMadeStatus(orderGoodsId, Constants.ORDER_GOODS_MADE_STATUS_ONGOING,userId,index);
    }

    /**
     * 完成制作
     *
     * @param orderGoodsId 订单ID
     * @param userId       用户id
     * @return {@link Response}<{@link Object}>
     */
    @ApiOperation(value="完成制作")
    @GetMapping("finish_cook")
    @LogAnno
    public Response<Object> finishCook(@LoginUser Integer userId,@ApiParam(name="orderGoodsId",value="订单商品id",required=true) @RequestParam Integer orderGoodsId,@RequestParam Integer index) {
    	return orderGoodsService.updateMadeStatus(orderGoodsId, Constants.ORDER_GOODS_MADE_STATUS_FINISHED,userId,index);
    }

    /**
    * 取消制作
     *
     * @param orderGoodsId 订单商品id
     * @param userId       用户id
     * @return {@link Response}<{@link Object}>
     */
    @ApiOperation(value="取消制作")
    @GetMapping("cancel_cook")
    @LogAnno
    public Response<Object> cancelCook(@LoginUser Integer userId,@ApiParam(name="orderGoodsId",value="订单商品id",required=true) @RequestParam Integer orderGoodsId,@RequestParam Integer index) {
        return orderGoodsService.updateMadeStatus(orderGoodsId, Constants.ORDER_GOODS_MADE_STATUS_NEW,userId,index);
    }

    /**
     * 配方
     * 订单商品配方
     *
     * @param orderGoodsId 订单id
     * @return {@link Response}<{@link OrderGoodsRecipeResponseVO}>
     */
    @ApiOperation(value="订单商品配方")
    @GetMapping("recipe")
    @LogAnno
    public Response<OrderGoodsRecipeResponseVO> recipe(@ApiParam(name="orderGoodsId",value="订单商品id",required=true) @RequestParam Integer orderGoodsId) {
    	return orderGoodsService.recipe(orderGoodsId,null);
    }


    /**
     * 订单细节
     *
     * @param orderId 订单id
     * @return {@link Object}
     */
    @ApiOperation(value="查询订单详情")
    @GetMapping("orderDetail")
    @LogAnno
    public Object orderDetail(@ApiParam(name="orderId",value="订单商品id",required=true) @RequestParam Integer orderId) {
        return orderGoodsService.orderDetail(orderId);
    }

    /**
     * 订单商品列表
     *
     * @param orderId 订单id
     * @return {@link Object}
     */
    @ApiOperation(value="查询订单商品")
    @GetMapping("orderGoodsList")
    @LogAnno
    public Object orderGoodsList(@ApiParam(name="orderId",value="订单商品id",required=true) @RequestParam Integer orderId) {
        return orderGoodsService.orderGoodsList(orderId);
    }

    /**
     * 订单产品细节
     *
     * @param orderGoodsId 订单商品id
     * @param goodsId      商品id
     * @return {@link Object}
     */
    @ApiOperation(value="查询订单商品详情")
    @GetMapping("orderGoodsDetail")
    @LogAnno
    public Object orderGoodsDetail(Integer orderGoodsId,Integer goodsId){
       return  orderGoodsService.orderGoodsDetail(orderGoodsId,goodsId);
    }


    @ApiOperation(value="厨房端 待制作订单商品")
    @GetMapping("outStandingInShop")
    @LogAnno
    public Object outStandingInShop(@ApiIgnore@LoginShop Integer shopId,
                                    @ApiIgnore @LoginUser Integer userId,
                                    @ApiParam(name = "page",value = "页",defaultValue = "1") Integer page,
                                    @ApiParam(name = "limit",value = "数量",defaultValue = "10") Integer limit){
        return  orderGoodsService.outStandingInShop(shopId,userId,page,limit);
    }

    @ApiOperation(value="制作商品")
    @PostMapping("update_cook")
    @LogAnno
    public Response updateCookInShop(@Validated @RequestBody  UpdateCookReqVO reqVO) {
        return orderGoodsService.updateCookInShop(reqVO);
    }


    @ApiOperation(value="厨房端小票详情")
    @GetMapping("getOrderGoodsDetailVos")
    @LogAnno
    public Object getOrderGoodsDetailVos(Integer orderGoodsId) {
        return ResponseUtil.ok(orderGoodsService.getOrderGoodsDetailVos(orderGoodsId));
    }

}
